package com.entity;

import java.math.BigDecimal;

/**
 * 折扣类促销
 * @author HeLiwei
 * 2023年9月4日
 */
public class DiscountPromotion extends Promotion {

	
	private BigDecimal discount;

	public BigDecimal getDiscount() {
		return discount;
	}

	public void setDiscount(BigDecimal discount) {
		this.discount = discount;
	}
	
	public DiscountPromotion() {}
	
	public DiscountPromotion(BigDecimal discount) {
		this.discount = discount;
	}

	/**
	 * 计算优惠金额 例如八折 200元 返回200*(1-0.8)=40 优惠40元
	 */
	@Override
	public BigDecimal getDeductPrice(Order order) {
		BigDecimal totalPrice = BigDecimal.ZERO;
		for (OrderItem orderItem : order.getOrderItemList()) {
			BigDecimal itemPrice = BigDecimal.ZERO;
			// 限定商品列表为空 或者 限定商品列表包含所购商品时享折扣
			if(this.getProductList()==null || this.getProductList().size()==0 || this.getProductList().contains(orderItem.getProduct())) {
				itemPrice = orderItem.getProduct().getPrice().multiply(new BigDecimal(orderItem.getQuantity()));
			}
			totalPrice = totalPrice.add(itemPrice);
		}
		return totalPrice.multiply(new BigDecimal("1").subtract(discount));
	}
}
